package com.ktjy.crm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.crm.mapper.RightMapper;
import com.ktjy.crm.model.Right;
import com.ktjy.crm.model.Role;
import com.ktjy.crm.service.RightService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.Serializable;
import java.util.List;

@SpringBootTest
public class RightServiceTester {
    @Resource
    private RightService rightService;
    @Resource
    private RightMapper rightMapper; // 注入 RightMapper
    @Test
    public void testInsert() {
        Right right = new Right("right_code", "right_parent_code", "right_type", "right_text", "right_url", "right_tip");
        int result = rightService.insert(right);
        System.out.println("result:" + result);
    }
    @Test
    public void testDelete() {
        int result = rightService.delete("right_code");
        System.out.println("result:" + result);
    }

    @Test
    public void testUpdate() {
        Right right = new Right("L0603", "right_parent_code", "right_type", "right_text", "right_url", "right_tip");
        int result = rightService.update(right);
        System.out.println("result:" + result);
    }
    @Test
    public void testSelect() {
        Right right = rightService.get("L0603");
        System.out.println("right:" + right);
    }

    @Test
    public void testFindAll() {
        List<Right> rights = rightService.findAll();
        System.out.println("rights:" + rights);
    }
    @Test
    public void testFind() {
        Role mockRole = rightService.getRoleWithRights(1L);
        System.out.println("mockRole:" + mockRole);
    }

    @Test
    public void testGet() {
        Right right = rightService.get("L0304");
        System.out.println("right_text: " + right.getRightText());
    }

    @Test
    public void testFindAdd() {
        List<Right> list = rightService.findAll();
        if (list != null) {
            for (Right right : list) {
                System.out.println("right_text: " + right.getRightText());
            }
        }
    }

    //分页
    @Test
    public void testPagination() {
        // 创建查询条件
        QueryWrapper<Right> queryWrapper = new QueryWrapper<>();

        // 分页对象，查询第1页，每页5条数据
        Page<Right> page = new Page<>(1, 5);

        // 执行分页查询
        IPage<Right> iPage = rightMapper.selectPage(page, queryWrapper);

        // 可选：输出分页信息以便调试
        System.out.println("当前页码：" + iPage.getCurrent());
        System.out.println("总页数"+iPage.getPages());
        System.out.println("每页数量：" + iPage.getSize());
        System.out.println("总记录数：" + iPage.getTotal());
        System.out.println("当前页数据：");
        for (Right right : iPage.getRecords()) {
            System.out.println(right);
        }
    }
    //执行更新操作
    @Test
    public void testUpdateAndVersion(){
        Right right=rightMapper.selectById("L01"); //读取用户数据
        right.setRightText("修改后的权限名称");

        int result=rightMapper.updateById(right);
        if (result==0){
            //更新失败 ：数据可能已经被其他用户修改
            System.out.println("更新失败 ：数据可能已经被其他用户修改");
        }else {
            //更新成功 MyBatis会自动将version字段+1
             System.out.println("更新成功 ："+right);
        }
    }
    //逻辑删除
    @Test
    public void testDeleteLogic(){
        rightMapper.deleteById("L0602");
    }
    @Test
    public void testSelectLogic(){
        Right u=new Right();
        u.setRightCode("L01");
        u.setRightText("权限名称");
        rightMapper.updateById(u);
    }
}
